package org.springframework.boot.autoconfigure.netty.channelInitializer;

import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;

public class HttpServerChannelInitialzer extends AbstractChannelInitializer {


	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
		/*
		HttpServerCodec 是 HttpRequestDecoder 和 HttpReponseEncoder 的组合，编码和解码的 handler
		pipeline.addLast("httpServerCodec", new HttpServerCodec());
		*/
		pipeline.addLast("http-decoder", new HttpRequestDecoder())
				// 将HTTP消息的多个部分合成一条完整的HTTP消息
				.addLast("http-aggregator", new HttpObjectAggregator(10*1024*1024))
				// 响应转码器
				.addLast("http-encoder", new HttpResponseEncoder())
				// 解决大码流的问题，ChunkedWriteHandler：向客户端发送HTML5文件
				.addLast("http-chunked", new ChunkedWriteHandler());

		super.initChannel(ch);

	}

}
